package com.testflow.hyh.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.testflow.hyh.entity.LoginDto;
import com.testflow.hyh.service.ILoginService;
import com.testflow.hyh.util.R;
import com.testflow.hyh.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements ILoginService {

    @Override
    public R login(LoginDto loginDto) {
        if(StringUtils.isEmpty(loginDto.getUsername())){
            return R.error(10000,"账号名不能为空");
        }
        if(StringUtils.isEmpty(loginDto.getPassword())){
            return R.error(10001,"密码不能为空");
        }
        if(!loginDto.getUsername().equals("admin")){
            return R.error(10002,"账号不存在");
        }
        if(!loginDto.getPassword().equals("123456")){
            return R.error(10003,"密码错误");
        }
        StpUtil.login(1,"PC");
        Map<String,Object> map = new HashMap<>();
        map.put("token",StpUtil.getTokenValue());
        map.put("authName",StpUtil.getTokenName());
        map.put("loginDevice",StpUtil.getLoginDevice());
        map.put("roles",StpUtil.getRoleList(1));
        map.put("permissions",StpUtil.getPermissionList(1));
        return R.success("登录成功",map);
    }
}
